package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.common.Const;
import com.top10.entity.Goods;
import com.top10.entity.GoodsDetailed;
import com.top10.entity.vo.GoodsWithIntCategoryVo;
import com.top10.entity.vo.SkuVo;
import com.top10.service.ICouponShopService;
import com.top10.service.IGoodsDetailedService;
import com.top10.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sny
 * @Package com.top10.controller.mobile
 * @date 2018/9/29
 */
@RestController
@RequestMapping("/top10/goods")
public class GoodController {
    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private ICouponShopService iCouponShopService;

    @Autowired
    private IGoodsDetailedService iGoodsDetailedService;
    /**
     * 团购推荐展示
     * @param pageBean
     * @return
     */
    @PostMapping("/boutiqueList")
    public ResponseBean boutiqueList(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Goods> goods=iGoodsService.selectList(new EntityWrapper<Goods>().eq("isBoutique", Const.IsBoutique)
                                                                             .eq("status",Const.IsStatus)
                                                                             .eq("recommend",Const.IsRecommend)
                                                                             .eq("isdeleted",Const.IsNotDelete)
                                                                            .ge("recommend_end",new Date()));
        pageBean= new PageBean(page.getPageNum(),page.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);

    }

    /**
     * 显示全球购列表
     * @param pageBean
     * @return
     */


    @PostMapping("/goodslist")
    public ResponseBean goodslist(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<GoodsWithIntCategoryVo> goodsWithIntCategoryVos=iGoodsService.selectglobalgoods();
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                .addData("pageBean",pageBean);
    }

    /**
     * 全球购推荐商品
     * @param pageBean
     * @return
     */

    @PostMapping("/qqqrecomend")
    public ResponseBean recomend(PageBean pageBean){

        Page page=PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());

        List<Goods> goods=iGoodsService.selectList(new EntityWrapper<Goods>().eq("status",Const.IsStatus)
                                                                             .eq("qqgBoutique",Const.IsRecommend)
                                                                             .ge("Recommend_end",new Date()));
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());

        return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);

    }

    /**
     * 根据分类uuid获取所有的商品，默认按销量降序排序
     * @param categoryuuid
     * @param pageBean
     * @param sortid
     * @return
     */
    @PostMapping("/listbycategory")
    public  ResponseBean listbycategory(String categoryuuid, PageBean pageBean, @RequestParam(value="sortid",defaultValue="1")long sortid){
        if(categoryuuid==null){
            return ResponseBean.createError("categoryuuid不能为空");
        }
        if(sortid==1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectgoodsbycategory(categoryuuid);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
       else{
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectgoodsbycategoryasc(categoryuuid);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }

    }

    /**
     * 根据分类uuid获取所有的商品，默认按价格降序排序
     * @param categoryuuid
     * @param pageBean
     * @param sortid
     * @return
     */
    @PostMapping("/listbyprice")
    public ResponseBean listbyprice(String categoryuuid,PageBean pageBean,@RequestParam(value="sortid",defaultValue="1")long sortid){
        if(categoryuuid==null){
            return ResponseBean.createError("categoryuuid不能为空");
        }

        if(sortid==1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectgoodsbyprice(categoryuuid);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
        else{
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectgoodsbypriceasc(categoryuuid);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }

    }


    /**
     * 根据分类id，查出包邮的商品
     * @param categoryuuid
     * @param pageBean
     * @return
     */
    @PostMapping("/listbyexpress")
    public ResponseBean listbyexpress(String categoryuuid,PageBean pageBean){

            if(categoryuuid==null){
                return ResponseBean.createError("categoryuuid不能为空");
            }
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectgoodsbyexpress(categoryuuid);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);

    }

    /**
     * 全球购，销售量排序
     * @param sortid
     * @param pageBean
     * @return
     */

    @PostMapping("/goodslistsort")
    public ResponseBean goodslistbysort(long sortid,PageBean pageBean){
        if(sortid==1){
                Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
                List<Map<String,Object>> goodsWithIntCategoryVos=iGoodsService.selectglobalgoodsbysort();
                pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
                return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                        .addData("pageBean",pageBean);
        }else {
            Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String,Object>> goodsWithIntCategoryVos=iGoodsService.selectglobalgoodsbysortasc();
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                    .addData("pageBean",pageBean);
        }

    }

    /**
     * 全球购价格排序
     * @param sortid
     * @param pageBean
     * @return
     */


    @PostMapping("/goodslistsortprice")
    public ResponseBean goodslistbysortprice(long sortid,PageBean pageBean){
        if(sortid==1){
            Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String,Object>> goodsWithIntCategoryVos=iGoodsService.selectglobalgoodsbyprice();
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                    .addData("pageBean",pageBean);
        }else {
            Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String,Object>> goodsWithIntCategoryVos=iGoodsService.selectglobalgoodsbypriceasc();
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                    .addData("pageBean",pageBean);
        }

    }


    /**
     * 全球购包邮
     * @param pageBean
     * @return
     */

    @PostMapping("/goodslistsortexpress")
    public ResponseBean goodslistsortexpress(PageBean pageBean){

            Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
            List<Map<String,Object>> goodsWithIntCategoryVos=iGoodsService.selectglobalgoodsbyexpress();
            pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",goodsWithIntCategoryVos)
                    .addData("pageBean",pageBean);


    }

    /**
     * 商店推荐商品
     * @param shopuuid
     * @param pageBean
     * @return
     */
   @PostMapping("/shoprecommend")
    public ResponseBean shoprecommend(String shopuuid,PageBean pageBean){

       Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
       List<Map<String,Object>> recommendgoods=iCouponShopService.selectshoprecommend(shopuuid);
       pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
       return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",recommendgoods)
               .addData("pageBean",pageBean);
   }

    /**
     * 商店根据销量排序
     * @param shopuuid
     * @param sortid
     * @param pageBean
     * @return
     */

    @PostMapping("/shopgoodsbysort")
    public ResponseBean shopgoodsbysort(String shopuuid,String goodsclassuuid, Long sortid,PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> recommendgoods=iCouponShopService.selectshopgoodsbysort(shopuuid,goodsclassuuid,sortid);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",recommendgoods)
                .addData("pageBean",pageBean);
    }


    @PostMapping("/shopallgoodsbysort")
    public ResponseBean shopassgoodsbysort(String shopuuid,Long sortid,PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> recommendgoods=iCouponShopService.selectshopallgoodsbysort(shopuuid,sortid);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goodsWithIntCategoryVos",recommendgoods)
                .addData("pageBean",pageBean);
    }



    @PostMapping("/goodcomment")
    public ResponseBean goodcomment(String shopuuid, String goodsclassuuid,PageBean pageBean){


        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> goodcomment=iCouponShopService.selectgoodcomment(shopuuid,goodsclassuuid);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goodcomment",goodcomment)
                .addData("pageBean",pageBean);


    }



    @PostMapping("/allgoodcomment")
    public ResponseBean allgoodcomment(String shopuuid, PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> goodcomment=iCouponShopService.selectallgoodcomment(shopuuid);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goodcomment",goodcomment)
                .addData("pageBean",pageBean);


    }

    /**
     * 今日最新
     * @param pageBean
     * @return
     */

    @PostMapping("/todaygoods")
    public ResponseBean todaygoods(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> todaygoods=iGoodsService.selecttoday();
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("todaygoods",todaygoods)
                .addData("pageBean",pageBean);

    }

    /***
     * 热卖产品
     * @param pageBean
     * @return
     */
    @PostMapping("/hotgoods")
    public ResponseBean hotgoods(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> todaygoods=iGoodsService.selecthot();
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("todaygoods",todaygoods)
                .addData("pageBean",pageBean);

    }

    /**
     * 新品到货
     * @param pageBean
     * @return
     */

    @PostMapping("/newgoods")
    public ResponseBean newgoods(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> todaygoods=iGoodsService.selectnew();
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("todaygoods",todaygoods)
                .addData("pageBean",pageBean);

    }

    /**
     * 今日好货
     * @param pageBean
     * @return
     */
    @PostMapping("/soldgoods")
    public ResponseBean soldgoods(PageBean pageBean){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> todaygoods=iGoodsService.selectsold();
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("todaygoods",todaygoods)
                .addData("pageBean",pageBean);

    }
    @PostMapping("/getshoprecommend")
    public ResponseBean getshoprecommend(PageBean pageBean,String shopUuid){

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> goods=iGoodsService.selectGetRecommendGoods(shopUuid);
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goods",goods)
                .addData("pageBean",pageBean);


    }
    /**
     * 商品搜索(全部商品)
     */
    @PostMapping("/searchallgoods")
    public ResponseBean getGoods(String keyword,PageBean pageBean){
        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String, Object>> goods=iGoodsService.selectAllGood(keyword);
        if(goods.size()==0){
            return ResponseBean.createSuccess("没有此类关键词的商品");
        }
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
    }

    /**
     *商品搜索(搜索出来的商品按价格排序)
     * @param
     * @return
     */
    @PostMapping("/searchallgoodsbyprice")
    public ResponseBean getGoodsOne(String keyword,PageBean pageBean,@RequestParam(value="sortid",defaultValue="1")long sortid){
        if(sortid==1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectallGoodByPriceOne(keyword);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
        else{
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectallGoodByPricessOne(keyword);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
    }

    /**
     * 商品搜索(销量排序)
     * @param
     * @return
     */
    @PostMapping("/searchallgoodsbysales")
    public ResponseBean getGoodstwo(String keyword,PageBean pageBean,@RequestParam(value="sortid",defaultValue="1")long sortid){
        if(sortid==1) {
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectallGoodBySalesOne(keyword);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
        else{
            Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
            List<Map<String, Object>> goods = iGoodsService.selectallGoodBySalessOne(keyword);
            pageBean = new PageBean(pageBean.getPageNum(), pageBean.getPageSize(), (int) page.getTotal());
            return ResponseBean.createSuccess().addData("goods",goods).addData("pageBean",pageBean);
        }
    }


    @PostMapping("/listStock")
    public ResponseBean listStock(String goodsuuid){
        GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",goodsuuid));
        if(goodsDetailed==null){
            return ResponseBean.createError("该商品不存在");
        }
        int  stock=goodsDetailed.getStock();
        return ResponseBean.createSuccess().addData("stock",stock);
    }
    @PostMapping("/getprice")
      public ResponseBean getPrice(String goodsuuid,@RequestParam(value="skukey",required = false)String skukey)throws Exception{
        GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",goodsuuid));
        SkuVo skuVo=new SkuVo();
      if(!skukey.equals("")){
          String[] line =skukey.split(",");
          Gson gson = new GsonBuilder().serializeNulls()
                  .setPrettyPrinting()
                  .disableHtmlEscaping()
                  .create();
          if(goodsDetailed.getSku()!=null){
              String sku= goodsDetailed.getSku();
              List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
              for (int i=0;i<=skuvo.size()-1;i++){
                  int c=-1;
                  for(int j=0;j<=line.length-1;j++){
                      if(skuvo.get(i).getKeyin().contains(line[j])){
                          c++;
                          if(c==line.length-1){
                              skuVo=skuvo.get(i);
                          }
                      }

                  }

              }
          }else {
              skuVo.setSkuprice(goodsDetailed.getPrice());
              skuVo.setSkustock(goodsDetailed.getStock());
          }

      }else{
          skuVo.setSkuprice(goodsDetailed.getPrice());
          skuVo.setSkustock(goodsDetailed.getStock());
      }
            return ResponseBean.createSuccess().addData("skuVo",skuVo);

    }
    @PostMapping("/getstatus")
    public ResponseBean getStatus(String goodsuuid){
        if(goodsuuid ==null){
            return ResponseBean.createError("请输入商品uuid");
        }
        Goods goods=iGoodsService.selectOne(new EntityWrapper<Goods>().eq("uuid",goodsuuid));
        if(goods.getStatus()!=1 || goods.getIsdeleted()){
            return ResponseBean.createError("此商品已下架或删除,请尽快申请退款");
        }
        return ResponseBean.createSuccess();
    }

}
